// pages/store/income/index.js
import util from '../../../utils/util.js';



Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        loaded: false,
        loading: true,
        page: 1,
        startDate: '',
        endDate: '',
        today: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中',
        });

        const date = new Date();
        console.log('date is: ', date)
        this.setData({
            startDate:  this.getFormatDate(date),
            endDate: this.getFormatDate(date),
            today: this.getFormatDate(date)
        })

        const that = this;
        var params = { paga: this.data.page, start_date: this.data.startDate, end_date: this.data.endDate };
        util.api.get('store/income', params, resp => {
            if (resp.status == true) {
                that.setData({
                    list: resp.data,
                    page: this.data.page + 1,
                    loading: false
                });
            }

            wx.hideLoading();
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    viewDetail: function (e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/store/income/detail/index?id=' + e.target.dataset.id,
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading();
        var that = this;
        this.setData({
            page: 1
        });

        var params = { paga: this.data.page, start_date: this.data.startDate, end_date: this.data.endDate };
        util.api.get('store/income', params, resp => {
            if (resp.status == true) {
                that.setData({
                    list: resp.data,
                    page: that.data.page + 1
                });
            }

            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        const that = this;
        if (this.data.loaded == true) {
            wx.showToast({
                title: '别拉了，到底了',
                icon: 'none'
            });
        } else {
            wx.showLoading({
                title: '玩命加载中',
            });

            var params = { paga: this.data.page, start_date: this.data.startDate, end_date: this.data.endDate };
            util.api.get('store/income', params, resp => {
                if (resp.status == true) {
                    if (resp.data.length < 1) {
                        wx.showToast({
                            title: '没有更多数据了',
                            icon: 'none'
                        });

                        that.setData({
                            loaded: true
                        });
                    } else {
                        that.setData({
                            list: that.data.list.concat(resp.data),
                            page: that.data.page + 1
                        });
                    }
                }

                setTimeout(function () {
                    wx.hideLoading();
                }, 1500)
            })
        }
    },

    bindStartDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            startDate: e.detail.value
        });
        this.onPullDownRefresh();
    },


    bindEndDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            endDate: e.detail.value
        });
        this.onPullDownRefresh();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    getFormatDate (date) {
        if (date instanceof Date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();

            if (month < 10) {
                month = '0' + month;
            }    
            if (day < 10) {
                day = '0' + day;
            }

            var day = `${year}-${month}-${day}`;
            return day;
        }
        return '';
    }
})